package com.salim.leetcode.$13;


import java.util.HashMap;
import java.util.Map;

public class RomantoInteger {

    /**
     * leetcode答案1
     * @param s
     * @return
     */
    public int leetcode1(String s) {
        int pre = 0;
        int res = 0;
        if(s.length() == 0) return res;

        //这个声明方式 太骚了
        int[] hash = new int[26];
        hash['I' - 'A'] = 1;
        hash['V' - 'A'] = 5;
        hash['X' - 'A'] = 10;
        hash['L' - 'A'] = 50;
        hash['C' - 'A'] = 100;
        hash['D' - 'A'] = 500;
        hash['M' - 'A'] = 1000;

        for(int i = s.length() - 1; i >= 0; i--)
        {
            int digit = hash[s.charAt(i) - 'A'];
            if(digit < pre)
                res -= digit;
            else
                res += digit;

            pre = digit;
        }

        return res;
    }

    /**
     * 硬编码
     * @param s
     * @return
     */
    public int leetcode2(String s) {
        int sum = 0;
        if (s.indexOf("IV") != -1) {
            sum -= 2;
        }
        if (s.indexOf("IX") != -1) {
            sum -= 2;
        }
        if (s.indexOf("XL") != -1) {
            sum -= 20;
        }
        if (s.indexOf("XC") != -1) {
            sum -= 20;
        }
        if (s.indexOf("CD") != -1) {
            sum -= 200;
        }
        if (s.indexOf("CM") != -1) {
            sum -= 200;
        }

        char[] romanArray = s.toCharArray();
        for (int i = 0; i < romanArray.length; i++) {
            char curr = romanArray[i];
            if (curr == 'I') {
                sum += 1;
            }
            if (curr == 'V') {
                sum += 5;
            }
            if (curr == 'X') {
                sum += 10;
            }
            if (curr == 'L') {
                sum += 50;
            }
            if (curr == 'C') {
                sum += 100;
            }
            if (curr == 'D') {
                sum += 500;
            }
            if (curr == 'M') {
                sum += 1000;
            }
        }

        return sum;
    }


    public int romanToInt(String s) {
        if (s == null || s.length() == 0) return 0;
        Map<Character,int[]> map = new HashMap<>();
        map.put('M', new int[]{1000, 0});
        map.put('D',new int[]{500, 1});
        map.put('C',new int[]{100, 2});
        map.put('L',new int[]{50, 3});
        map.put('X',new int[]{10, 4});
        map.put('V',new int[]{5, 5});
        map.put('I',new int[]{1, 6});

        int[] lastResult = map.get(s.charAt(0));
        int sum = lastResult[0];
        for(int i =1;i<s.length();i++){
            int[] result = map.get(s.charAt(i));

            //如果上一个大于等于你的阶级 做加法
            if(lastResult[1]<=result[1]){
                sum += result[0];
            }else{
                //如果上一个小于你的阶级 先加后减
                sum = sum + result[0] - 2*lastResult[0];
            }
            lastResult = result;
        }
        return sum;
    }

    public static void main(String[] args){
        String[][] test = {
                {"VI","VIII","IX","XI","XIV","XV","XVIII","XIX","XX","XXX","XL","L", "LX","LXX","LXXX","XC","C",  "CC", "CCC","CD", "D",  "M"},
                {"6", "8",   "9", "11","14", "15","18",   "19", "20","30", "40","50","60","70", "80",  "90","100","200","300","400","500","1000"}};
        RomantoInteger romantoInteger = new RomantoInteger();
        for(int i=0;i<test[0].length;i++){
            if(!String.valueOf(romantoInteger.romanToInt(test[0][i])).equals(test[1][i])){
                System.out.println("序号："+i+" 转化前："+test[0][i]+ " 转化后："+String.valueOf(romantoInteger.romanToInt(test[0][i])));
            }
        }
    }
}
